var vm = new Vue({
    el: '#rrapp',
    data: {
        title: '',//编辑标题
        storeGoodsCategoryTwd: [], //数据
        q: {categoryName: '', categoryType: '', beginTime: '', endTime: ''},
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0}, //分页(定义)
        dialogFormVisible: false,
        multipleSelection: [],//多选
        categoryFrom: {
            categoryType: '',
            categoryName: '',
            categoryOrder: ''
        },
        //重置按钮
        resetForm(categoryFrom) {
            this.$refs[categoryFrom].resetFields();
        }
    },
    created: function () { //加载数据
        this.loadData();
    },
    methods: {
        //每页显示数据量变更
        handleSizeChange: function (val) {
            vm.page.pageSize = val;
            this.loadData();
        },
        //当前页面
        handleCurrentChange: function (val) {
            vm.page.currentPage = val;
            this.loadData();
        },
        //搜索
        search: function () {
            this.page.currentPage = 1; //查询的页码
            this.loadData();
        },
        //重置
        reset() {
            vm.q.categoryName = '';
            vm.q.categoryType = '';
            vm.q.beginTime = '';
            vm.q.endTime = '';
            this.loadData();
        },
        //获取后台数据
        loadData: function () {
            //查询参数
            var params = this.q;
            // 分页参数
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "storeGoodsCategory/storegoodscategorytwd/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.storeGoodsCategoryTwd = r.page.list;
                        vm.page.total = r.page.totalCount
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //新增
        handleAdd: function () {
            this.title = "新 增";
            vm.categoryFrom = {};
            this.dialogFormVisible = true;
        },
        //编辑
        handleEdit: function (index, row) {
            this.title = "编 辑";
            var rowData = JSON.parse(JSON.stringify(row));
            vm.categoryFrom = Object.assign({}, rowData);
            this.dialogFormVisible = true;
        },
        //取消清空验证
        closeChangeMachie() {
            vm.dialogFormVisible = false;
             this.$refs['categoryFrom'].resetFields();
        },

        //新增-编辑-提交
        submitForm(categoryFrom) {
            var postData = {
                categoryId: vm.categoryFrom.categoryId,
                categoryType: vm.categoryFrom.categoryType,
                categoryName: vm.categoryFrom.categoryName,
                categoryOrder: vm.categoryFrom.categoryOrder
            };
            var url;
            var _this = this;
            if (isBlank(postData.categoryId)) {
                url = "storeGoodsCategory/storegoodscategorytwd/save";
            } else {
                url = "storeGoodsCategory/storegoodscategorytwd/update"
            }
            this.$refs[categoryFrom].validate(function (valid) {
                if (valid) {
                    //输入为负数，不写入数据库
                    if (!validNumber(vm.categoryFrom.categoryOrder)) {
                        _this.$message.error('请输入大于0的整数数字');
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.dialogFormVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                }, 200);
                               setTimeout(function () {
                                   _this.loadData();
                               },500)
                            } else {
                                alert(r.msg);
                            }
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //单条删除
        handleDelete: function (index, row) {
            var id = row.categoryId;
            var _this = this;
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "storeGoodsCategory/storegoodscategorytwd/delete?id=" + id,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '删除成功!'
                            }, 200);
                            _this.loadData();

                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })

        },
        //多条删除
        handleAllDelete: function () {
            var selected = this.$refs.multipleTable.selection;
            var ids = [];
            var _this = this;
            for (var index in selected) {
                ids.push(selected[index].categoryId);
            }
            if (ids.length == 0) {
                this.$message.error('最少选择一条数据 !');
                return false;
            }
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                var postData = ids.join(',');
                $.ajax({
                    type: "POST",
                    url: baseURL + "storeGoodsCategory/storegoodscategorytwd/deleteAll?ids=" + postData,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code === 0) {
                            _this.$message({
                                type: 'success',
                                message: '删除成功!'
                            }, 200);
                            _this.loadData();
                        } else {
                            alert(r.msg);
                        }
                    }
                })
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })

        },
        //序号
        indexMethod: function (index) {
            return index + 1;
        },
        //格式化分类类型
        typeFormatter(val) {
            if (val.categoryType == "fastStore") {
                return "数码图文店"
            } else {
                return "广告物料店"
            }
        }
    }
});

//验证数字为大于0的非负整数
function validNumber(str) {
    var reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}

//控制提交间隔3s
function testButton() {
    var aaa = document.getElementById('click1');
    aaa.disabled = true; //使用true或false，控制是否让按钮禁用
    setTimeout(function () {
        aaa.disabled = false;
    }, 3000);//3秒后才能点击
};